<script>
	var swfu_
	$(function(){
	var btn = $("#upbtn").get(0);
    swfu_= new SWFUpload({
    		upload_url: "/upload/upload",
            file_post_name: "filedata",
			file_size_limit : "1 MB",
			file_types : "*.jpg;*.png;*.jpeg'*.bmp",
			file_types_description : "bmp,jpeg,jpg,png图片",
			file_upload_limit :5,
			post_params : {
	           "bizType" : "OTHER",
	           "fileType" : "IMAGE",
	           "fileName" : ""
		    },
			swfupload_preload_handler : preLoad,
			swfupload_load_failed_handler : loadFailed,
			file_queue_error_handler : fileQueueError,
			file_dialog_complete_handler : fileDialogComplete,
			upload_progress_handler : uploadProgress,
			upload_error_handler : uploadError,
			upload_success_handler : function(f,data){
				eval("var json="+data+";");
				if(json.message=="Success!"){
					var htmstr="";
					htmstr+="<div style='float:left;'>";
					htmstr+='<input type="hidden" name="coverUrl" value="'+json.data.urls+'">';
					htmstr+='<img height="100px"src="'+json.data.urls+'"/>';
					htmstr+='<br/><a href="#" class="easyui-linkbutton" onclick="javascript:$(this).parent().remove();"><b>删除</b></a>';
					htmstr+='</div>';
					$("#img_div").append(htmstr);
					/*$('.easyui-linkbutton').linkbutton({    
					    iconCls: 'icon-remove'
					});*/ 
				}else{
					$.messager.alert('警告','文件上传出错');
				}
			},
			upload_complete_handler : uploadComplete,
			// Button Settings
			button_placeholder_id:"upbtn",
			button_image_url : "/img/SmallSpyGlassWithTransperancy_17x18.png",
			button_width: 80,
			button_height: 18,
			button_text : '<span class="button">上传图片</span>',
			button_text_style : '.button {font-size: 12pt; }',
			button_text_top_padding: 0,
			button_text_left_padding: 18,
			button_disabled : false,
			button_window_mode: SWFUpload.WINDOW_MODE.TRANSPARENT,
			button_cursor: SWFUpload.CURSOR.HAND,
			// Flash Settings
			flash_url : swfuploaddir+"/swfupload.swf",
			flash9_url : swfuploaddir+"/swfupload_fp9.swf",
			custom_settings : {
			//	upload_target : "divFileProgressContainer",
				thumbnail_height: 500,
				thumbnail_width: 500,
				thumbnail_quality: 100
			},
			debug: true
		});
    
	});
</script>

<script type="text/javascript">
var marketId = $("#market_id").val();
$(function(){
	$("#form_camera").datagrid({
		title:'对应视频列表',
		url:'/mediaSource/findMediaSource',
		queryParams: {
			marketId : marketId,
			bizType : "MARKET"
		},
		columns:[[
			{field:'marketName',title:'市场名称',width:120},
			{field:'media_type',title:'视频类型',width:60,align:'center',
	      		formatter:function(value,row,index){
		      		if(row.media_type=='VIDEO'){
		      			return "录播"
		      		} else if(row.media_type=='LIVE'){
		      			return "直播"
		      		}
				}
			},
			{field:'liveName',title:'直播摄像头',width:110}, 
			{field:'liveStatus',title:'直播状态',width:60,align:'center',
	      		formatter:function(value,row,index){
		      		if(row.liveStatus==1 && row.media_type=='LIVE'){
		      			return "在线"
		      		}else if(row.liveStatus==0 && row.media_type=='LIVE'){
		      			return "不在线"
		      		}else{
		      			return "";
		      		} 
				}
			},
			{field:'videoName',title:'录播名称',width:100},
			{field:'vid',title:'录播VID',width:150},
			/* {field:'sort',title:'排序',width:120},  */
			{field:'cover',title:'视频封面',width:60,
			        formatter:function(value,row,index){
			        	if(value)
			        	return "<img src='"+value+"'  height='30px'/>";
			        }
		    },
			{field:'cz',title:'操作',
				formatter:function(value,row,index){
					return "<a onclick='deleteMediaSource(" + row.id + ")' class='linkbutton'>删除</a> <a class='linkbutton' onclick='updateMediaInfo(" + index + ")'>编辑</a>";
			    }
			}
		]],
	    toolbar : [],
	    pagination:false,
	    onDblClickRow:function(rowIndex, rowData){
			$(this).datagrid('unselectAll');
			$(this).datagrid('selectRow', rowIndex);
		},
	    onLoadSuccess:function(data){
			$(".linkbutton").linkbutton();
		}
	});
	
	$("#cameraIds").combotree({    
		url: '/camera/combotree/',  
	    multiple:true,
    	editable:false,
	    width:200
	});
	
	$("#videoIds").combotree({    
		url: '/video/combotree/',  
	    multiple:true,
    	editable:false,
	    width:200
	});
});

function deleteMediaSource(id){
	$.messager.confirm('请确认', '是否删除该视频？', function(r) {
		if (r) {
			$.ajax({
				url: "/mediaSource/deleteMediaSource",
				data: {"id":id},
					dataType: "json",
					success : function(r) {
					if (r.success) {
						$("#form_camera").datagrid('load');
						$("#form_camera").datagrid('unselectAll');
						$.messager.show({
							msg:r.msg,
							title:'提示'
						});
						editRow = undefined;
					} else {
						$.messager.alert("操作错误",r.msg,'error');
					}
				}
			});
		}
	});
}

function submitMediaSource() {
	var marketId = $("#market_id").val();
	var biz_type = "MARKET";
	var cameraIds = $("#cameraIds").combotree("getValues")+"";
	var videoIds = $("#videoIds").combotree("getValues")+"";
	
	if(cameraIds=='' && videoIds==''){
		alert("请至少选择一个直播或录播视频");
		return;
	}
	$.ajax({
		url: "/mediaSource/savaMediaSource",
		data: {"marketId":marketId,"bizType":biz_type,"cameraIds":cameraIds,"videoIds":videoIds},
			dataType: "json",
			success : function(r) {
			if (r.success) {
				$("#form_camera").datagrid('load');
				$("#form_camera").datagrid('unselectAll');
				$.messager.show({
					msg:r.msg,
					title:'提示'
				});
				editRow = undefined;
			} else {
				$.messager.alert("操作错误",r.msg,'error');
			}
		}
	});
}
</script>
<div title="视频" style="padding:5px;">
	<input id="market_id" type="hidden" name="market_id"  value="${(po.id)!}"/>
	<table class="tableForm tableCss" id="project_income_table">
		<tr>
			<th><font color=red>*</font>市场名称</th>
			<td colspan="6">${(po.name)!}</td>
		</tr>
		<tr>
			<th><font color=red>*</font>直播视频</th>
			<td><input id="cameraIds" name="cameraIds" value=""/></td>
			<th><font color=red>*</font>录播视频</th>
			<td><input id="videoIds" name="videoIds" value=""/></td>
		</tr>
		<tr>
			<td colspan="6" align="center">
				<a onclick="javascript:submitMediaSource();" class="easyui-linkbutton" data-options="iconCls:'icon-save'">保存</a>
			</td>
		</tr>
	</table>
	<table id="form_camera"></table> 
</div>

<!-- 编辑封面和排序 -->
<div id="carema_edit_dialog" style="display: none;width: -100px;height: -100px;">
	<p><b><span style="margin-left: 5px;">修改视频封面</span></b></p>
	<form id="carema_edit_form" method="post" action="/mediaSource/updateMediaSource">
	   <input type="hidden" name="mediaId" id="mediaId" value="">
	   <table class="tableForm tableCss">
		 <!-- <tr>
	       <th>排序（填数值）</th>
		   <td colspan="1"><input type="text" name="sort" id="sortId" class="easyui-validatebox" value="0" style="width: 40px;">&nbsp;数值越大越靠后</td>
	     </tr> -->
	     <tr>
			<th><font color=red>*</font>分类背景图片</th>
			<td>
				<div title="图片" style="padding:5px;">
					<div class="tableForm datagrid-toolbar" >
						<div style="width:60px;border:1px;">
							<span id="upbtn"></span>
						</div>
						<div id="img_div">
							<div style='float:left;'>
								<input type="hidden" name="coverUrl" id="coverUrl" value="">
								<img id="coverImgId" height="100px"/>
								<br/>
								<a href="#" class="easyui-linkbutton" onclick="javascript:$(this).parent().remove();"><b>删除</b></a>
							</div>
						</div>
					</div>
				</div>
			</td>
		</tr>
	  </table>
    </form>
</div>
<script type="text/javascript">
function updateMediaInfo(index){
	$("#form_camera").datagrid('clearSelections');
	$("#form_camera").datagrid('selectRow',index);
	var rowData = $("#form_camera").datagrid('getSelections');
	
	$("#carema_edit_dialog").show();
	$("#carema_edit_form").form("clear");
	
	$("#mediaId").val(rowData[0].id);
	//$("#sortId").val(rowData[0].sort);
	$("#coverUrl").val(rowData[0].cover);
	$("#coverImgId").attr("src",rowData[0].cover);
	
	$("#carema_edit_dialog").dialog({   
    title: '编辑',   
    width: 450,   
    height: 330,   
 	inline:true,
    closed: false,   
    cache: false, 
    buttons:[
		{
			text:'确定',
			iconCls:'icon-ok',
			handler:function(){
				$("#carema_edit_form").form('submit',{
					success:function (r){
						r = eval( '(' + r + ')' );//将字符串转化为json
						if (r.success){
							$("#carema_edit_dialog").dialog("close");
							
							$("#form_camera").datagrid('reload');
							$("#form_camera").datagrid('unselectAll');
							
							$.messager.show({
								msg:r.msg,
								title:'提示'
							});
						}
						else{
							$.messager.alert("操作错误",r.msg,'error');
						}
					},
					error: function (error){
						alert("error:"+error);
						$.messager.alert('提示', error ,'error');
					}
				})
			}
		},
		{text:'取消',iconCls:'icon-cancel',
			handler:function(){
				$("#carema_edit_dialog").dialog("close");
			}
		}],
		onClose : function (){
			 $('.validatebox-tip').remove();
		},
		onMove : function (left,top){//移动时判断是否超出边界
			if (left <= 0){
				$(this).dialog('resize',{left:0});
			}
			if (top < 0){
				$(this).dialog('resize',{top:0});
			}
		}
    })  
}
</script>
